/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine.internal;

import com.googlecode.boringengine.Font;
import com.googlecode.boringengine.GameLoader;
import com.googlecode.boringengine.Log;
import com.googlecode.boringengine.Render;
import com.googlecode.boringengine.Sprite;
import java.util.HashMap;
import java.util.Scanner;

public class ImageFont implements Font {
    private int letterSpacing, wordSpacing;
    private HashMap<Character, Sprite> letters;
    private Sprite unknownLetter;

    public ImageFont(String fileName, GameLoader l) {
        Log.debug("Loading font '%s'...", fileName);
        Scanner in = new Scanner(l.getFile(fileName));
        String[] headerTokens = in.nextLine().split(" ");
        letters = new HashMap<Character, Sprite>();
        if (headerTokens.length != 3 || !headerTokens[0].equals("font")) {
            Log.error("'%s' is not a font!", fileName);
            letterSpacing = wordSpacing = 2;
            unknownLetter = Render.createSprite(DEF_UNKNOWN_PIXELS, DEF_UNKNOWN_WIDTH, DEF_UNKNOWN_HEIGHT);
            return;
        }
        letterSpacing = Integer.parseInt(headerTokens[1])*2;
        wordSpacing = Integer.parseInt(headerTokens[2])*2;
        String chars = in.nextLine();
        int charPos = 0;
        String line, spriteStr="";
        int w = 0, h = 0;
        while (in.hasNextLine()) {
            line = in.nextLine();
            if (line.equals("")) {
                letters.put(chars.charAt(charPos), Render.createSprite(spriteStr, w, h));
                spriteStr = "";
                w = h = 0;
                charPos++;
                continue;
            }
            spriteStr += line;
            w = line.length();
            h++;
        }
        if (!spriteStr.equals(""))
            unknownLetter = Render.createSprite(spriteStr, w, h);
        if (unknownLetter == null)
            unknownLetter = Render.createSprite(DEF_UNKNOWN_PIXELS, DEF_UNKNOWN_WIDTH, DEF_UNKNOWN_HEIGHT);
    }

    public void draw(int x, int y, String str) {
        int oldx = Render.getX(), oldy = Render.getY();
        Render.moveOverWithoutScale(x, y);
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            if (ch == ' ') {
                Render.moveOverWithScale(wordSpacing, 0);
                continue;
            }
            Sprite toDrawWith;
            if (letters.containsKey(ch))
                toDrawWith = letters.get(ch);
            else
                toDrawWith = unknownLetter;
            Render.drawSprite(toDrawWith);
            Render.moveOverWithScale(toDrawWith.getWidth() + letterSpacing, 0);
        }
        Render.setPosition(oldx, oldy);
    }

    public int getWidth(String str) {
        int width = 0;
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            if (ch == ' ') {
                width += wordSpacing;
                continue;
            }
            Sprite toDrawWith;
            if (letters.containsKey(ch))
                toDrawWith = letters.get(ch);
            else
                toDrawWith = unknownLetter;
            width += toDrawWith.getWidth() + letterSpacing;
        }
        return width;
    }

    private static String DEF_UNKNOWN_PIXELS =
            "0000000" +
            "0999990" +
            "0990090" +
            "0909090" +
            "0900990" +
            "0999990" +
            "0000000";
    private static int DEF_UNKNOWN_WIDTH = 7, DEF_UNKNOWN_HEIGHT = 7;
}
